//
//  BJForgetPassViewController.m
//  DangXiao
//
//  Created by APPLE on 16/12/8.
//  Copyright © 2016年 APPLE. All rights reserved.
//

#import "BJForgetPassViewController.h"

#import "BJSecondForgetPassViewController.h"

@interface BJForgetPassViewController ()
@property (nonatomic, strong) NSString *testString;
@end

@implementation BJForgetPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    
}

//static BJTextAndLineView *phoneViewTF;
//static BJTextAndLineView *codeViewTF;
//static UIButton *getCodeBtn;
static UIButton *tipBtn;
//static BJLabelAndLineView *phoneView;
//static BJLabelAndLineView *codeView;
-(void)initUI{
    
//    [UIImage imageNamed:<#(nonnull NSString *)#>]
    tipBtn = [Utils createBtnWithType:(UIButtonTypeCustom) backgroundColor:ClearColor title:@"手机号码不正确" image:@"tanhao" font:14];
    tipBtn.enabled = NO;
    tipBtn.titleLabel.textAlignment = 0;
    [tipBtn setTitleColor:BaseColor forState:(UIControlStateNormal)];
    [self.view addSubview:tipBtn];
    [tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(BILIHEIGHT(20) + 64);
        make.left.mas_equalTo(BILIWIDTH(30));
        make.size.mas_equalTo(CGSizeMake(BILIWIDTH(200), BILIHEIGHT(20)));
    }];
    
    tipBtn.hidden = YES;
    
    _phoneView = [[BJLabelAndLineView alloc]init];
    [self.view addSubview:_phoneView];
    [_phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipBtn.mas_bottom).offset(BILIHEIGHT(10));
        make.left.mas_equalTo(BILIWIDTH(30));
        make.size.mas_equalTo(CGSizeMake(BILIWIDTH(80), BILIHEIGHT(30)));
    }];

    _codeView = [[BJLabelAndLineView alloc]init];
    [self.view addSubview:_codeView];
    _codeView.nameLabel.text = @"验证码";
    [_codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneView.mas_bottom).offset(BILIHEIGHT(20));
        make.left.mas_equalTo(BILIWIDTH(30));
        make.size.equalTo(_phoneView);
    }];
    
    _phoneViewTF = [[BJTextAndLineView alloc]init];
    _phoneViewTF.nameTF.clearButtonMode = YES;
    [_phoneViewTF.nameTF addTarget:self action:@selector(didInputPhoneNum:) forControlEvents:(UIControlEventTouchDown)];
    [self.view addSubview:_phoneViewTF];
    [_phoneViewTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_phoneView.mas_right).offset(BILIWIDTH(10));
        make.top.equalTo(_phoneView);
        make.right.mas_equalTo(-BILIWIDTH(30));
        make.height.mas_equalTo(BILIHEIGHT(30));
//        make.width.mas_equalTo(BILIWIDTH(260));
    }];
    
    _codeViewTF = [[BJTextAndLineView alloc]init];
    _codeViewTF.nameTF.clearButtonMode = YES;
    [_codeViewTF.nameTF addTarget:self action:@selector(didInputCodeNum:) forControlEvents:(UIControlEventTouchDown)];
    _codeViewTF.nameTF.placeholder = @"请输入验证码";
    [self.view addSubview:_codeViewTF];
    [_codeViewTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_codeView.mas_right).offset(BILIWIDTH(10));
        make.top.equalTo(_codeView);
        make.width.mas_equalTo(BILIWIDTH(125));
        make.height.mas_equalTo(BILIHEIGHT(30));
    }];
    
    _getCodeBtn = [Utils createBtnWithType:(UIButtonTypeCustom) backgroundColor:ClearColor title:@"获取验证码" image:nil font:15];
    [_getCodeBtn addTarget:self action:@selector(didClickGetCodeBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [_getCodeBtn setTitleColor:BaseColor forState:UIControlStateNormal];
    [self.view addSubview:_getCodeBtn];
    BJViewBorderRadius(_getCodeBtn, 3.0, 1, BaseColor);
    [_getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_codeViewTF.mas_right).offset(BILIWIDTH(10));
        make.top.equalTo(_codeView);
        make.right.mas_equalTo(-BILIWIDTH(30));
        make.height.mas_equalTo(BILIHEIGHT(30));
    }];
    
    _nextBtn = [Utils createBtnWithType:(UIButtonTypeCustom) backgroundColor:BaseColor title:@"下一步" image:nil font:15];
    [_nextBtn addTarget:self action:@selector(didClickNextBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_nextBtn];
    BJViewBorderRadius(_nextBtn, 3.0, 1, BaseColor);
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(BILIWIDTH(30));
        make.top.equalTo(_codeViewTF.mas_bottom).offset(BILIHEIGHT(45));
        make.right.mas_equalTo(-BILIWIDTH(30));
        make.height.mas_equalTo(BILIHEIGHT(35));
    }];
}

#pragma mark ---- 点击“下一步”按钮
- (void)didClickNextBtn:(UIButton *)btn{
//    if ([self doClickNextBtn]) {
        BJSecondForgetPassViewController *VC = [[BJSecondForgetPassViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
//    }
}

#pragma mark ---- 开始输入手机号码
- (void)didInputPhoneNum:(UITextField *)tf{
    tipBtn.hidden = YES;
    _phoneViewTF.lineLabel.backgroundColor = GrayLabel;
    _phoneView.lineLabel.backgroundColor = GrayLabel;
}

#pragma mark ---- 开始输入验证码
- (void)didInputCodeNum:(UITextField *)tf{
    tipBtn.hidden = YES;
    _codeViewTF.lineLabel.backgroundColor = GrayLabel;
    _codeView.lineLabel.backgroundColor = GrayLabel;
}

#pragma mark ---- 点击“获取验证码”按钮
- (void)didClickGetCodeBtn:(UIButton *)btn{
    if (_phoneViewTF.nameTF.text.length == 0) {
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入手机号码" forState:0];
        _phoneViewTF.lineLabel.backgroundColor = BaseColor;
        _phoneView.lineLabel.backgroundColor = BaseColor;
       
    }else if (![Utils validatePhone:_phoneViewTF.nameTF.text]){
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入正确的手机号码" forState:0];
       _phoneViewTF.lineLabel.backgroundColor = BaseColor;
        _phoneView.lineLabel.backgroundColor = BaseColor;
    }else{
        [self time];
        [self requestMessage];
    }
}

- (BOOL)doClickNextBtn{
    if (_phoneViewTF.nameTF.text.length == 0) {
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入手机号码" forState:0];
        _phoneViewTF.lineLabel.backgroundColor = BaseColor;
        _phoneView.lineLabel.backgroundColor = BaseColor;
        return NO;
    }else if (![Utils validatePhone:_phoneViewTF.nameTF.text]){
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入正确的手机号码" forState:0];
        _phoneViewTF.lineLabel.backgroundColor = BaseColor;
        _phoneView.lineLabel.backgroundColor = BaseColor;
        return NO;
    }else if (_codeViewTF.nameTF.text.length == 0){
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入验证码" forState:0];
        _codeViewTF.lineLabel.backgroundColor = BaseColor;
        _codeView.lineLabel.backgroundColor = BaseColor;
        return NO;
    }else if (![_codeViewTF.nameTF.text isEqualToString:self.testString]){
        tipBtn.hidden = NO;
        [tipBtn setTitle:@"请输入正确的验证码" forState:0];
        _codeViewTF.lineLabel.backgroundColor = BaseColor;
        _codeView.lineLabel.backgroundColor = BaseColor;
        return NO;
    }
    return YES;
}

#pragma mark ---- 倒计时
- (void)time{
    
    __block int timeout=59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_getCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                
                _getCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            // int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_getCodeBtn setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                
                _getCodeBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

//请求验证码
- (void)requestMessage{
    
    NSInteger code = 100000 + arc4random() % (1000000 - 100000 + 1);
    NSString * content = [NSString stringWithFormat:@"%ld",(long)code];
    self.testString = [NSString stringWithString:content];
    DLog(@"验证码是%@",self.testString);
    NSString * urlPath = [NSString stringWithFormat:CodeMessage,_codeViewTF.nameTF.text,content];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:urlPath parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DLog(@"123");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"%@",error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
